<template>
  <el-dialog
    :title="formData.id ? '编辑配置' : '新增配置'"
    :visible="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    width="800px"
    center
  >
    <el-form
      ref="addEditForm"
      v-loading="isLoading > 0"
      :model="formData"
      :rules="rules"
      label-width="100px"
      class="add-edit-form"
    >
      <el-form-item label="客服电话" prop="phone">
        <el-input v-model="formData.phone" />
      </el-form-item>
      <el-form-item label="活动">
        <ul class="activity-goods-box">
          <li v-for="(item, index) in formData.activity_list" :key="item.img">
            <upload-img v-model="item.img" />
            <el-input v-model="item.to" class="input" placeholder="请输入用户点击该图片去往的页面" />
            <i
              class="icon el-icon-remove"
              @click="removeNewGoodsItem(formData.activity_list, index)"
            />
            <i
              class="icon el-icon-circle-plus"
              @click="addNewGoodsItem(formData.activity_list, index)"
            />
          </li>
        </ul>
      </el-form-item>
      <el-form-item label="充值规则">
        <ul class="recharge-box">
          <li v-for="(item, index) in formData.recharge_money" :key="item.img">
            <el-input v-model="item.money" class="input" placeholder="请输入充值金额" />
            <el-input v-model="item.give" class="input" placeholder="请输入赠送金额" />
            <i
              class="icon el-icon-remove"
              @click="removeNewGoodsItem(formData.recharge_money, index)"
            />
            <i
              class="icon el-icon-circle-plus"
              @click="addNewRechargeItem(formData.recharge_money, index)"
            />
          </li>
        </ul>
      </el-form-item>
      <el-form-item label="是否上线">
        <el-switch
          :value="formData.status === 1"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="formData.status = formData.status === 1 ? -1 : 1"
        />
      </el-form-item>

      <el-form-item class="submit-box">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button v-if="!formData.id" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import _ from 'lodash'
const formData = {
  phone: '',
  activity_list: [{ img: '', to: '' }],
  recharge_money: [{ money: null, give: null }],
  status: -1
}
import UploadImg from '@/components/UploadImg'
import { addOneConfig, updateOneConfigById } from '@/api/config'
export default {
  name: 'ConfigAddEdit',
  components: {
    UploadImg
  },
  props: {},
  data() {
    return {
      dialogVisible: false,
      isLoading: 0,
      formData: {},
      rules: {

      }
    }
  },
  computed: {},
  watch: {},
  created() {
  },
  mounted() {},
  methods: {
    removeNewGoodsItem(arr, i) {
      if (arr.length === 1) {
        this.$message.error('最后一个不能删')
        return
      }
      arr.splice(i, 1)
    },
    addNewGoodsItem(arr, i) {
      arr.splice(i + 1, 0, { img: '', to: '' })
    },
    addNewRechargeItem(arr, i) {
      arr.splice(i + 1, 0, { money: null, give: null })
    },
    handleClose() {
      this.dialogVisible = false
    },
    open(row) {
      this.dialogVisible = true
      this.formData = row ? _.cloneDeep(row) : _.cloneDeep(formData)
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    resetForm() {
      this.formData = { ...formData }
      this.$refs.addEditForm.resetFields()
    },
    submitForm() {
      if (this.isLoading > 0) return
      this.$refs.addEditForm.validate(async vaild => {
        if (!vaild) return
        try {
          this.isLoading++

          if (this.formData.id) {
            await updateOneConfigById(this.formData)
          } else {
            await addOneConfig(this.formData)
          }
          this.$emit('refresh')
          this.handleClose()
        } finally {
          this.isLoading--
        }
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.submit-box {
  display: flex;
  justify-content: flex-end;
}

.new-goods-box,
.activity-goods-box,
.recharge-box {
  li {
    display: flex;
    align-items: center;
  }
  .input {
    margin: 20px;
  }
  .icon {
    cursor: pointer;
    font-size: 30px;
    color: #409eff;
  }
}
</style>
